package produce_consume;

import java.util.LinkedList;
import java.util.Queue;

class TaskQueue1<T> {
    private Queue<T> queue;
    private static final int DEFAULT_CAPACITY = 10;
    private final int MAX_CAPACITY;

    public TaskQueue1() {
        this(DEFAULT_CAPACITY);
    }

    public TaskQueue1(int capacity) {
        this.MAX_CAPACITY = capacity;
        this.queue = new LinkedList<>();
    }

    public void offer(T e) throws InterruptedException {
        synchronized (queue) {
            while (queue.size() >= MAX_CAPACITY) {
                queue.wait();
            }
            queue.add(e);
            queue.notifyAll();
        }
    }

    public T poll() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                queue.wait();
            }
            queue.notifyAll();
            return queue.poll();
        }
    }
}